package com.example.webapp.controler;

import com.example.webapp.bean.User;
import com.example.webapp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
public class UpdateControler {
    @Autowired
    private UserService userService;

    @CrossOrigin
    @PostMapping("/update")
    @ResponseBody
    public String update(@RequestBody Map map){
        Integer UpId= Integer.parseInt(((String) map.get("id")));
        Optional<User> Res=userService.selectUserid(UpId);
        User OldUser;

        if (!Res.isPresent()) {
            return "cannot";
        } else {
            OldUser=Res.get();
        }
        OldUser.setEmail((String) map.get("email"));
        if(((String) map.get("gender")).equals("男")){
            OldUser.setGender(1);
            System.out.println(1);
        }else{
            OldUser.setGender(0);
            System.out.println(0);
        }


        OldUser.setPhone((String) map.get("phone"));
        OldUser.setName((String) map.get("name"));
        userService.updateUser(OldUser);
        return "success";
    }
}
